package com.example.moban.view.dialog;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.example.moban.R;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;


import butterknife.BindView;

/**
 *
 * @author yql
 * @date: 2018/10/12.
 */
public class VersionDownloadDialog extends SimpleDialogFragment {

    @BindView(R.id.version_download_dialog_content)
    AppCompatTextView dialogTitleText;
    @BindView(R.id.horizontal_progress_bar)
    ProgressBar progressBar;

    private String url, path;
    private Handler handler = new Handler();
    private DownloadListener listener;

    @Override
    public void setContentView(@NonNull LayoutInflater inflater, FrameLayout contentView, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.version_download_dialog_layout, contentView, false);
        contentView.addView(view);
    }

    @Override
    public void initView(View view) {
        dialogTitleText.setText( getResources().getString( R.string.downloading ));
    }

    @Override
    public void initData() {
        downloadFile( url , path );
    }

    public void showDialog(FragmentManager manager, String url , String path , DownloadListener listener ) {
        this.url = url;
        this.path = path;
        this.listener = listener;
        show(manager, TAG);
    }

    /**
     * 开始下载文件
     * @param url
     * @param path
     */
    public void downloadFile( String url , String path ){
        FileDownloader.getImpl()
                .create(url)
                .setWifiRequired(true)
                .setPath( path )
                .setListener(new FileDownloadListener() {
                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        int percent = (int) ((double) soFarBytes / (double) totalBytes * 100);
                        if( progressBar != null ){
                            progressBar.setProgress( percent );
                        }
                    }

                    @Override
                    protected void blockComplete(BaseDownloadTask task) {

                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        dismiss();
                        listener.completed();
                    }

                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        dismiss();
                        listener.error();
                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {
//                continueDownLoad(task);//如果存在了相同的任务，那么就继续下载
                    }
                }).start();
    }

    public interface DownloadListener{
        void completed();
        void error();
    }
}
